

/**
 * 掩码
 */
export class Mask {
    private _mask: Uint32Array;

    constructor() {
        this._mask = new Uint32Array(1);
    }

    public set(type: number): void {
        const index = Math.floor(type / 32);
        const bit = type % 32;
        this._mask[index] |= 1 << bit;
    }

    public unset(type: number): void {
        const index = Math.floor(type / 32);
        const bit = type % 32;
        this._mask[index] &= ~(1 << bit);
    }

    public has(type: number): boolean;
    public has(otherMask: Mask): boolean;
    public has(type: Mask | number): boolean {
        if (typeof type === "number") {
            return !!(this._mask[((type / 31) >>> 0)] & (1 << (type % 31)));
        }
        for (let i = 0; i < this._mask.length; i++) {
            if ((this._mask[i] & type._mask[i]) !== type._mask[i]) {
                return false;
            }
        }
        return true;
    }

    public clear():void {
        for (let i = 0; i < this._mask.length; i++) {
            this._mask[i] = 0;
        }
    }
}

